﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LPV_POS.Module;
using System.Globalization;
using System.Resources;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang9_TienMat_The : Form
    {
        int y = 0;
        float iTong_Tien = 0;
        string maPhieu = "";

        //--bc theo httt, editor: PL, date: 2012.07.25
        thuvien tv;
        string[] s;
        object[] o;
        public static string hinhthucthanhtoan = "";
        public static string tenthe = "";
        //--end


        public mh_banhang9_TienMat_The(float _iTong_Tien, string _maPhieu)
        {//409, 225
            InitializeComponent();
            y = this.ClientSize.Height;
            this.ClientSize = new Size(this.ClientSize.Width, 200);
            iTong_Tien = _iTong_Tien;
            maPhieu = _maPhieu;
        }


        CultureInfo culture;
        string str_lang = "";

        #region "Muti language  --editor: PL, date: 2012.07.25"

        private void SetLanguage_mhbanhang9_Tienmat_The(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);

                lblTienMat.Text = rm.GetString("tienmat", culture);
                lblTheTraTruoc.Text = rm.GetString("the", culture);
                label3.Text = rm.GetString("loaithe", culture);
                btnThanhToan.Text = rm.GetString("thanhtoan", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion


        DataTable tbDM_THE_TRA_TRUOC = null;
        private void mh_banhang6_PhuThu_Load(object sender, EventArgs e)
        {
            string sSQL = "SELECT ttt.DM_THE_TRA_TRUOC_ID, ttt.DM_THE_TRA_TRUOC_CODE, ttt.NGAY_LAP, ttt.TIEN " +
                          "FROM DM_THE_TRA_TRUOC ttt";
            tbDM_THE_TRA_TRUOC = new thuvien().SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            //--bc theo httt, editor: PL, date: 2012.07.25
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_mhbanhang9_Tienmat_The(str_lang);
            //end 2012.07.25
        }

        private void lblTienMat_Click(object sender, EventArgs e)
        {
            hinhthucthanhtoan = "Tiền mặt";//--bc theo httt, editor: PL, date: 2012.07.25
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void lblTheTraTruoc_Click(object sender, EventArgs e)
        {
            this.ClientSize = new Size(this.ClientSize.Width, y);
            //--bc theo httt, editor: PL, date: 2012.07.25
            cbo_LoaiThe.DataSource = f_GetAll();    
            cbo_LoaiThe.DisplayMember = "OBJNAME";  
            cbo_LoaiThe.ValueMember = "OBJID";     

            cbo_LoaiThe.Text = "------ Chọn thẻ thanh toán ------";
            //end 2012.07.25
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            ////kiem tra
            //if (lblngay.Text == "")
            //{
            //    MessageBox_LPV.Show("Nhập thẻ cần thanh toán");
            //    return;
            //}
            //if ((float)nudTien.Value < iTong_Tien)
            //{
            //    MessageBox_LPV.Show("Số tiền trong thẻ không đủ");
            //    return;
            //}
            //this.Cursor = Cursors.WaitCursor;
            //thuvien othuvien = new thuvien();

            //int irt = othuvien.SP_Update("sp_DM_THE_TRA_TRUOC_TRU_TIEN",
            //                             new string[] { "@flag", "@DM_THE_TRA_TRUOC_TRU_TIEN_ID", "@DM_THE_TRA_TRUOC_ID", 
            //                                            "@NGAY", "@TIEN", "@TIEN_SAU", "@BH_PHIEU_MA" },
            //                             new object[] { 1,-1, iDM_THE_TRA_TRUOC_ID,
            //                                            null, iTong_Tien, ((float)nudTien.Value - iTong_Tien), maPhieu}
            //                            );
            //this.Cursor = Cursors.Default;

            //if (irt <= 0)
            //{
            //    MessageBox_LPV.Show("Lưu thất bại");
            //    return;
            //}
            //this.DialogResult = DialogResult.Cancel;
            //this.Close();

            //bc httt, editor: PL, date: 2012.07.25
            hinhthucthanhtoan = "Thẻ";
            tenthe = cbo_LoaiThe.Text;
            string st_temp = cbo_LoaiThe.SelectedValue.ToString();

            int kq = f_InsertBH_THE(maPhieu, st_temp, tenthe);
            //end 2012.07.25

            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void calcEdit2_EditValueChanged(object sender, EventArgs e)
        {
            lblngay.Text = "";
            nudTien.Value = 0;
            iDM_THE_TRA_TRUOC_ID = -1;
            if (calcEdit2.Value > 0)
            {
                if (tbDM_THE_TRA_TRUOC.Select("DM_THE_TRA_TRUOC_CODE = " + calcEdit2.Value + "").Length > 0)
                {
                    DateTime dngaylap;
                    DateTime.TryParse(tbDM_THE_TRA_TRUOC.Select("DM_THE_TRA_TRUOC_CODE = '" + calcEdit2.Value + "'")[0]["NGAY_LAP"] + "", out dngaylap);
                    lblngay.Text = dngaylap.ToString("dd/MM/yyyy");

                    float itien = 0;
                    float.TryParse(tbDM_THE_TRA_TRUOC.Select("DM_THE_TRA_TRUOC_CODE = '" + calcEdit2.Value + "'")[0]["TIEN"] + "", out itien);
                    nudTien.Value = (decimal)itien;

                    int.TryParse(tbDM_THE_TRA_TRUOC.Select("DM_THE_TRA_TRUOC_CODE = " + calcEdit2.Value + "")[0]["DM_THE_TRA_TRUOC_ID"] + "", out iDM_THE_TRA_TRUOC_ID);
                }
            }
        }
        int iDM_THE_TRA_TRUOC_ID = -1;


        #region '  bc theo httt, editor: PL, date: 2012.07.25'
        private DataTable f_GetAll()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            DataTable tb = tv.SP_SelectDataTable("sp_mh_dmTheTT", s, o);

            return tb;
        }

        private int f_InsertBH_THE(string str_maPhieu, string str_maThe, string str_tenThe)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@BH_PHIEU_MA", "@OBJID", "@OBJNAME" };
            o = new object[] { 2, 1, str_maPhieu, str_maThe, str_tenThe };
            int tb = tv.SP_Update("sp_mh_dmTheTT", s, o);

            return tb;
        }
        #endregion

    }
}